//
//  VRHomeTopView.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/14.
//

import UIKit

class VRHomeTopView: UIView {

    private let searchButton         = c_make_ex(UIButton.self)
        .image("Home_search").end()
    private let hotButton         = c_make_ex(UIButton.self)
        .text("Hot").color("#FFFC00",state: .selected).font(18.font_bold)
        .color(.white.alpha(0.9)).end()
    private let discoverButton         = c_make_ex(UIButton.self)
        .text("Discover").color("#FFFC00",state: .selected).font(18.font_bold)
        .color(.white.alpha(0.9)).end()
    private let multiButton         = c_make_ex(UIButton.self)
        .text("Multi-Person").color("#FFFC00",state: .selected).font(18.font_bold)
        .color(.white.alpha(0.9)).end()
    private let hotImg         = c_make_ex(UIImageView.self)
        .image("Discover_check").end()
    private let discoverImg         = c_make_ex(UIImageView.self)
        .image("Discover_check").end()
    private let multiImg         = c_make_ex(UIImageView.self)
        .image("Discover_check").end()
    
    var hotBlock: (() -> Void)?
    var discoverBlock: (() -> Void)?
    var multiBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            searchButton
            hotButton
            discoverButton
            multiButton
            hotImg
            discoverImg
            multiImg
        }
        
        searchButton.size(40).top(2).trailing(5)
        hotButton.leading(0).width(60).height(23)
        hotButton.CenterY == searchButton.CenterY
        discoverButton.leading(60).width(95).height(23)
        discoverButton.CenterY == searchButton.CenterY
        multiButton.leading(165).width(130).height(23)
        multiButton.CenterY == searchButton.CenterY
        hotImg.width(20).height(7)
        hotImg.CenterX == hotButton.CenterX
        hotImg.Top == hotButton.Bottom + 2
        discoverImg.width(20).height(7)
        discoverImg.CenterX == discoverButton.CenterX
        discoverImg.Top == discoverButton.Bottom + 2
        multiImg.width(20).height(7)
        multiImg.CenterX == multiButton.CenterX
        multiImg.Top == multiButton.Bottom + 2
        
        
        hotButton.isSelected = true
        hotImg.isHidden = false
        discoverButton.isSelected = false
        discoverImg.isHidden = true
        multiButton.isSelected = false
        multiImg.isHidden = true
        
        searchButton.addTarget(self, action: #selector(clickSearchButton), for: .touchUpInside)
        hotButton.addTarget(self, action: #selector(clickHotButton), for: .touchUpInside)
        discoverButton.addTarget(self, action: #selector(clickDiscoverButton), for: .touchUpInside)
        multiButton.addTarget(self, action: #selector(clickMultiButton), for: .touchUpInside)
    }
    
    @objc private func clickSearchButton(){

        currentViewController().present(VRHomeSearchViewController(), animated: true)
    }
    
    @objc private func clickHotButton(){
        
        if !hotButton.isSelected {
            
            hotButton.isSelected = true
            discoverButton.isSelected = false
            multiButton.isSelected = false
            hotImg.isHidden = false
            discoverImg.isHidden = true
            multiImg.isHidden = true
            
            hotBlock?()
        }
    }
    
    @objc private func clickDiscoverButton(){
        
        if !discoverButton.isSelected {
            
            hotButton.isSelected = false
            discoverButton.isSelected = true
            multiButton.isSelected = false
            hotImg.isHidden = true
            discoverImg.isHidden = false
            multiImg.isHidden = true
            
            discoverBlock?()
        }
    }
    
    @objc private func clickMultiButton(){
        
        if !multiButton.isSelected {
            
            hotButton.isSelected = false
            discoverButton.isSelected = false
            multiButton.isSelected = true
            hotImg.isHidden = true
            discoverImg.isHidden = true
            multiImg.isHidden = false
            
            multiBlock?()
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
